//
//  MessageAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/12/5.
//

#import "MessageAlertView.h"

@interface MessageAlertView()
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIButton *noBtn;
@property(nonatomic,strong)UIButton *yesBtn;

@end

@implementation MessageAlertView

-(instancetype)initWithFrame:(CGRect)frame andModel:(NewsListModel*)model{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        self.model = model;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)weakSelf = self;
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(weakSelf.clickBlock){
            weakSelf.clickBlock();
        }
    }];
    [self addGestureRecognizer:tap];
    
    CGFloat b_w = 280;
    if(self.currentAvailableWidth < 312){
        b_w  = self.currentAvailableWidth-32;
    }
    CGFloat content_h = [self getStringHeightWithText:self.model.content font:JPUIFontPingFangSCRegular(14) viewWidth:b_w-32];
    CGFloat b_h = 90+content_h;
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.userInteractionEnabled = NO;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(b_w);
        make.height.mas_equalTo(b_h);
        make.centerX.mas_equalTo(weakSelf);
        make.centerY.mas_equalTo(weakSelf);
    }];
    self.bgView = bgView;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(16);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.text = @"消息提醒";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@20);
        make.height.mas_equalTo(@22);
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
    }];
    
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.font = JPUIFontPingFangSCRegular(14);
    self.contentLabel.textColor = KFontGrayColor;
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.text = self.model.content;
    [bgView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(20);
    }];
    
    
}

-(void)refreshWidth{
    CGFloat b_w = 280;
    if(self.currentAvailableWidth < 312){
        b_w  = self.currentAvailableWidth-32;
    }
    CGFloat content_h = [self getStringHeightWithText:self.model.content font:JPUIFontPingFangSCRegular(14) viewWidth:b_w-32];
    CGFloat b_h = 90+content_h;
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(b_w);
        make.height.mas_equalTo(b_h);
    }];
}

-(void)setModel:(NewsListModel *)model{
    _model = model;
    self.contentLabel.text = model.content;
    CGFloat b_w = 280;
    if(self.currentAvailableWidth < 312){
        b_w  = self.currentAvailableWidth-32;
    }
    CGFloat content_h = [self getStringHeightWithText:self.model.content font:JPUIFontPingFangSCRegular(14) viewWidth:b_w-32];
    CGFloat b_h = 90+content_h;
    [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(b_h);
    }];
}


@end
